package com.example.syssoft.service;

import com.example.syssoft.common.service.LogicService;
import com.example.syssoft.config.Result;
import com.example.syssoft.dao.*;
import com.example.syssoft.domain.*;
import com.example.syssoft.domain.build.ShopItemBuilderImpl;
import com.example.syssoft.domain.dto.*;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class ShopService extends LogicService<ShopDao, Shop,Long> {

    @Resource
    ItemDao itemDao;

    @Resource
    ShopItemDao shopItemDao;

    @Resource
    OrderDao orderDao;

    public ShopService(ShopDao lr) {
        super(lr);
    }

    @Transactional
    public Result addItem(Long shopId, Item item){
        Shop shop = getDAO().getReferenceById(shopId);
        Item newItem = itemDao.save(item);
        ShopItem shopItem = shop.addItem(newItem);
//        System.out.println();
        getDAO().save(shop);
        return new Result(true,shopItem);
    }

    @Transactional
    public Result saleItem(Long shopId, Long itemId){
        Item item = new Item();
        item.setId(itemId);
        Shop shop = new Shop();
        shop.setId(shopId);
        ShopItem shopItemByItemAndShop = shopItemDao.findShopItemByItemAndShop(item, shop);
        if(shopItemByItemAndShop==null)
            return new Result("无此商品");
        shop.saleItem(shopItemByItemAndShop);
        ShopItem save = shopItemDao.save(shopItemByItemAndShop);
        if(save==null){
            return new Result("上架失败");
        }
        return new Result(true,save);
    }

    @Transactional
    public Result stopSale(Long shopId, Long itemId){
        Item item = new Item();
        item.setId(itemId);
        Shop shop = new Shop();
        shop.setId(shopId);
        ShopItem shopItemByItemAndShop = shopItemDao.findShopItemByItemAndShop(item, shop);
        if(shopItemByItemAndShop==null)
            return new Result("无此商品");
        shop.stopSale(shopItemByItemAndShop);
        ShopItem save = shopItemDao.save(shopItemByItemAndShop);
        if(save==null){
            return new Result("下架失败");
        }
        return new Result(true,save);
    }

    public Result getAllShop(int page ,int size){
        Pageable pageable = PageRequest.of(page,size, Sort.by("id").descending());
        Page<Shop> all = getDAO().findAll(pageable);
        List<Shop> content = all.getContent();
        List<ShopDto> newList = new ArrayList<>();

        for (int i = 0; i <content.size() ; i++) {
            ShopDto shopDto = new ShopDto();
            Business business = content.get(i).getBusiness();
            BusinessDto businessDto = new BusinessDto();
            BeanUtils.copyProperties(content.get(i),shopDto);
            BeanUtils.copyProperties(business,businessDto);
//            shopDto.setBusinessDto(businessDto);
            newList.add(shopDto);
        }
        return new Result(true,newList);
    }

    public Result getShopByBusinessId(int page , int size ,Long businessId){
        Business business = new Business();
        business.setId(businessId);
        Shop shop = new Shop();
        shop.setBusiness(business);
        Pageable pageable = PageRequest.of(page,size, Sort.by("id").descending());
        Example<Shop> shopExample = Example.of(shop);
        List<Shop> content = getDAO().findAll(shopExample, pageable).getContent();
        List<ShopDto> shopDtos = new ArrayList<>();

        for (int i = 0; i < content.size() ; i++) {
            ShopDto shopDto = new ShopDto();
            Business bus = content.get(i).getBusiness();
            BusinessDto businessDto = new BusinessDto();
            BeanUtils.copyProperties(content.get(i),shopDto);
            BeanUtils.copyProperties(bus,businessDto);
//            shopDto.setBusinessDto(businessDto);
            shopDtos.add(shopDto);
        }

        return new Result(true,shopDtos);
    }

    //查询未上架商品
    public Result getAllLineItemByShopIdState0(Long shopId){
//        ShopItem shopItem = new ShopItem();
        Shop shop = new Shop();
        shop.setId(shopId);
        ShopItem shopItem = new ShopItemBuilderImpl()
                .withShop(shop)
                .withState(0)
                .build();
//        shopItem.setShop(shop);
//        shopItem.setState(0);
        Example<ShopItem> shopItemExample = Example.of(shopItem);
        List<ShopItem> all = shopItemDao.findAll(shopItemExample);

        List<ItemDto> allItemDto = new ArrayList<>();
        for (int i = 0; i <all.size() ; i++) {
            Item item = all.get(i).getItem();
            ItemDto itemDto = new ItemDto();
            BeanUtils.copyProperties(item,itemDto);
            allItemDto.add(itemDto);
        }
        return new Result(true,allItemDto);
    }

    //查询上架商品
    public Result getAllLineItemByShopIdState1(Long shopId){
        ShopItem shopItem = new ShopItem();
        Shop shop = new Shop();
        shop.setId(shopId);
        shopItem.setShop(shop);
        shopItem.setState(1);
        Example<ShopItem> shopItemExample = Example.of(shopItem);
        List<ShopItem> all = shopItemDao.findAll(shopItemExample);

        List<ItemDto> allItemDto = new ArrayList<>();
        for (int i = 0; i <all.size() ; i++) {
            Item item = all.get(i).getItem();
            ItemDto itemDto = new ItemDto();
            BeanUtils.copyProperties(item,itemDto);
            allItemDto.add(itemDto);
        }
        return new Result(true,allItemDto);
    }

    public Result getOrderByShopId(Long shopId){
        Shop shop = new Shop();
        shop.setId(shopId);
        List<Order> res = orderDao.findOrdersByShop(shop);
        List<OrderDto> orderDtos = new ArrayList<>();
        if(res==null)
            return new Result("无订单");
        for (int i = 0; i <res.size() ; i++) {
            OrderDto orderDto = new OrderDto();
            List<LineItemDto> lineItemDto = new ArrayList<>();
            BeanUtils.copyProperties(res.get(i),orderDto);
            System.out.println(res.get(i).getLineItems().size());
            for (int j = 0; j <res.get(i).getLineItems().size() ; j++) {

                LineItemDto itemDto = new LineItemDto();
                itemDto.setItemName(res.get(i).getLineItems().get(j).getItem().getName());
                itemDto.setTotalPrice(res.get(i).getLineItems().get(j).getTotalPrice());
                itemDto.setCount(res.get(i).getLineItems().get(j).getCount());
                itemDto.setId(res.get(i).getLineItems().get(j).getId());
                lineItemDto.add(itemDto);
            }
            orderDto.setLineItemDto(lineItemDto);
//            orderDto.setLineItems(null);
            orderDtos.add(orderDto);
//            lineItemDto.clear();
        }

        return new Result(true,orderDtos);
    }

    @Transactional
    public Result acceptOrder(Long orderId){
        Order orderById = orderDao.findOrderById(orderId);
        if(orderById.getPayState() == 0){
            return new Result(false,"该订单未付款");
        }
        orderById.acceptOrder();
        orderDao.save(orderById);
        return new Result(true,orderById);
    }

    @Transactional
    public Result refuseOrder(Long orderId){
        Order orderById = orderDao.findOrderById(orderId);

        orderById.refuseOrder();
        orderDao.save(orderById);
        return new Result(true,orderById);
    }

    public Result getAllOrderState1(Long shopId,Integer state){
        Shop shop = new Shop();
        shop.setId(shopId);
        List<OrderDto> orderDtos = new ArrayList<>();
        List<Order> res = orderDao.findOrderByShopAndState(shop,state);
        List<LineItemDto> lineItemDto = new ArrayList<>();
        for (int i = 0; i <res.size() ; i++) {
            OrderDto dto = new OrderDto();
            for (int j = 0; j <res.get(i).getLineItems().size() ; j++) {
                LineItemDto itemDto = new LineItemDto();
                itemDto.setItemName(res.get(i).getLineItems().get(j).getItem().getName());
                itemDto.setTotalPrice(res.get(i).getLineItems().get(j).getTotalPrice());
                itemDto.setCount(res.get(i).getLineItems().get(j).getCount());
                itemDto.setId(res.get(i).getLineItems().get(j).getId());
                lineItemDto.add(itemDto);
            }

            BeanUtils.copyProperties(res.get(i),dto);
            dto.setLineItemDto(lineItemDto);
            dto.setShopName(res.get(i).getShop().getName());
            orderDtos.add(dto);
        }


//        dto.setLineItems(null);
        if(res==null)
            return new Result("无订单");
        else
            return new Result(true,orderDtos);
    }

    public Result getAllOrderState2(Long shopId,Integer state,Integer payState){
        Shop shop = new Shop();
        shop.setId(shopId);
        List<OrderDto> orderDtos = new ArrayList<>();
        List<Order> res = orderDao.findOrderByShopAndStateAndPayState(shop,state,payState);
        List<LineItemDto> lineItemDto = new ArrayList<>();
        for (int i = 0; i <res.size() ; i++) {
            OrderDto dto = new OrderDto();
            for (int j = 0; j <res.get(i).getLineItems().size() ; j++) {
                LineItemDto itemDto = new LineItemDto();
                itemDto.setItemName(res.get(i).getLineItems().get(j).getItem().getName());
                itemDto.setTotalPrice(res.get(i).getLineItems().get(j).getTotalPrice());
                itemDto.setCount(res.get(i).getLineItems().get(j).getCount());
                itemDto.setId(res.get(i).getLineItems().get(j).getId());
                lineItemDto.add(itemDto);
            }

            BeanUtils.copyProperties(res.get(i),dto);
            dto.setLineItemDto(lineItemDto);
            dto.setShopName(res.get(i).getShop().getName());
//            dto.setBusinessName(res.get(i).get);
            orderDtos.add(dto);
        }


//        dto.setLineItems(null);
        if(res==null)
            return new Result("无订单");
        else
            return new Result(true,orderDtos);
    }

    public Result getAllOrder(Long shopId){
        Shop shop = new Shop();
        shop.setId(shopId);
        List<OrderDto> orderDtos = new ArrayList<>();
        List<Order> res = orderDao.findOrderByShop(shop);
        List<LineItemDto> lineItemDto = new ArrayList<>();
        for (int i = 0; i <res.size() ; i++) {
            OrderDto dto = new OrderDto();
            for (int j = 0; j <res.get(i).getLineItems().size() ; j++) {
                LineItemDto itemDto = new LineItemDto();
                itemDto.setItemName(res.get(i).getLineItems().get(j).getItem().getName());
                itemDto.setTotalPrice(res.get(i).getLineItems().get(j).getTotalPrice());
                itemDto.setCount(res.get(i).getLineItems().get(j).getCount());
                itemDto.setId(res.get(i).getLineItems().get(j).getId());
                lineItemDto.add(itemDto);
            }

            BeanUtils.copyProperties(res.get(i),dto);
            dto.setLineItemDto(lineItemDto);
            dto.setShopName(res.get(i).getShop().getName());
            orderDtos.add(dto);
        }


//        dto.setLineItems(null);
        if(res==null)
            return new Result("无订单");
        else
            return new Result(true,orderDtos);
    }

}
